# -*- coding: utf-8 -*-
# run.py
from flask import Flask,render_template,redirect,request
#from flask_flatpages import FlatPages
from celery import Celery

FLATPAGES_AUTO_RELOAD = True
FLATPAGES_ROOT = 'pages'
FLATPAGES_EXTENSION = '.md'

def make_celery(app):
    celery=Celery(__name__,broker='redis://10.10.100.21:6379/0')
    return celery

app = Flask(__name__)
#app.config.from_object(__name__)
#flatpages = FlatPages(app)

app.config.update(
    CELERY_BROKER_URL='redis://10.10.100.21:6379/0',
    CELERY_RESULT_URL='redis://10.10.100.21:6379/0',
    CELERY_TASK_SERIALIZER='json',
    CELERY_RESULT_SERIALIZER='json',
    #CELERY_TIMEZONE = 'America/Los_Angeles',
    #CELERY_ENABLE_UTC = True,
    #CELERY_IMPORTS = ("tasks",)
)
celery = make_celery(app)
celery.conf.update(app.config)

@celery.task
def add_together(a,b):
    return a+b

@celery.task
def sendmail(mail):
    print('sending mail to %s...' % mail['to'])
    #time.sleep(2.0)
    print('mail sent.')
    return mail['to']


#定义过滤器
from views.filters import filter
app.register_blueprint (filter)

# #前台
# from views.homes import home
# app.register_blueprint(home)
@app.route('/send/',methods=['GET','POST'])
def app_sendmail():
    if request.method=='GET':
        return render_template('send.html')

    if request.method=='POST':
        email=request.form.get('email')
        if email:
            #tasks.sendmail({'to':email})
            ret=sendmail.delay({'to': email})
            print ret
        return redirect('/send/')


# 后台
from views.admins import admin
app.register_blueprint(admin, url_prefix='/admin')

# 后台任务
from views.tasks import task
app.register_blueprint(task, url_prefix='/task')

if __name__ == '__main__':
    app.run(port=5089)


